﻿using BlueportCoffee.CoffeeModel;
using BlueportCoffee.IceModel;
using BlueportCoffee.MilkTeaModel;
using BlueportCoffee.model;
using BlueportCoffee.RobotModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Media.Imaging;
using System.Xml.Linq;

namespace BlueportCoffee.SocketModel
{
    public class SocketClients
    {
        private static SocketClients instance;
        public bool cupbol1 = false;
        public List<ClientList> clientLists = new List<ClientList>();
        private SocketClients() { }
        public static SocketClients Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SocketClients();
                }
                return instance;
            }
        }
        /// <summary>
        /// 创建客户端
        /// </summary>
        public Socket client;
        public void socketclient()
        {
            try
            {
                ///创建客户端
                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                ///IP地址
                IPAddress ip = IPAddress.Parse(IPorPortInfo.Instance.CoffeeIP);
                ///端口号
                IPEndPoint endPoint = new IPEndPoint(ip, 8885);
                ///建立与服务器的远程连接
                client.Connect(endPoint);
                ClientList clientList = new ClientList();
                clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList.code = 4;
                client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                ///线程问题
                Thread thread = new Thread(ReciveMsg);
                thread.IsBackground = true;
                thread.Start(client);
                Thread thread1 = new Thread(MsgShowServer);
                thread1.IsBackground = true;
                thread1.Start();
                Thread thread2 = new Thread(IOMsg.Instance.IOSerial_Thr);
                thread2.IsBackground = true;
                thread2.Start();
                Thread thread3 = new Thread(IOMsgShow.Instance.IOSerial_Thr);
                thread3.IsBackground = true;
                thread3.Start();
                Thread thread4 = new Thread(coffeeMsg.Instance.IOSerial_Thr);
                thread4.IsBackground = true;
                thread4.Start();
                Thread thread8 = new Thread(IceMakingMsg.Instance.IOSerial_Thr);
                thread8.IsBackground = true;
                thread8.Start();
                Thread thread9 = new Thread(MilkTeaSocket.Instance.Connect);
                thread9.IsBackground = true;
                thread9.Start();
                Thread.Sleep(1000);
                Thread thread6 = new Thread(IOSerial_Robot_Thr);
                thread6.IsBackground = true;
                thread6.Start();
            }
            catch (Exception)
            {
                return;
            }
        }
        public void IOSerial_Robot_Thr()
        {
            try
            {
                bool isRobot = false;
                RobotSerialPortLeft.Instance.Initialization(IPorPortInfo.Instance.RobotIPLeft, IPorPortInfo.Instance.RobotPortLeft);
                Thread.Sleep(1000);
                RobotModelLeft.Instance.RobotJQRSNL_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotJQRSNL_UP();
                Thread.Sleep(100);
                while (!isRobot)
                {
                    isRobot = RobotModelLeft.Instance.LRobotJQRYSN_Sel();
                    Thread.Sleep(100);
                }
                Thread.Sleep(1000);
                RobotModelLeft.Instance.RobotJQRSNL_Down();
                Thread.Sleep(100);
                ClientList clientList = new ClientList();
                clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList.code = 12;
                client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                RobotModelLeft.Instance.QK();
                Thread thread5 = new Thread(MainWindow.MessageShow);
                thread5.IsBackground = true;
                thread5.Start();
               
                //RobotModelLeft.Instance.RobotJQRXDL_Down();
                //Thread.Sleep(100);
                //RobotModelLeft.Instance.RobotJQRSDL_UP();
                //Thread.Sleep(1000);
                //RobotModelLeft.Instance.RobotJQRSDL_Down();
                //Thread.Sleep(100);
                ////RobotModelLeft.Instance.RobotJQRSNL_UP();
                ////Thread.Sleep(100);
                //while (!isRobot)
                //{
                //    isRobot = RobotModelLeft.Instance.LRobotJQRYSN_Sel();
                //    Thread.Sleep(100);
                //}
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// 客户端接收到服务器发送的消息
        /// </summary>
        /// <param name="o">客户端</param>
        void ReciveMsg(object o)
        {
            Socket client = o as Socket;
            while (true)
            {
                try
                {
                    ///定义客户端接收到的信息大小
                    byte[] arrList = new byte[1024 * 1024];
                    ///接收到的信息大小(所占字节数)
                    int length = client.Receive(arrList);
                    string msg = Encoding.UTF8.GetString(arrList, 0, length);
                    var ClientList = JsonConvert.DeserializeObject<ClientList>(msg);
                    if (ClientList != null)
                    {
                        clientLists.Add(ClientList);
                    }
                }
                catch (Exception)
                {
                    ///关闭客户端
                    client.Close();
                }

            }
        }
        public void MsgShowServer()
        {

            try
            {
                while (true)
                {
                    try
                    {
                        if (clientLists.Count > 0)
                        {
                            var ClientList = clientLists[0];
                            if (ClientList != null)
                            {
                                if (ClientList.code == 0)
                                {
                                    show(ClientList.message.type, ClientList.message.Name);
                                }
                                else if (ClientList.code == 3)
                                {
                                    RobotShow(ClientList.message.Name, ClientList.message.type);
                                    ClientList clientList = new ClientList();
                                    clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                                    clientList.robotMsg = RobotMsg.Instance;
                                    clientList.code = 3;
                                    //clientList.IsMake = MainWindow.isMake;
                                    var list = JsonConvert.SerializeObject(clientList);
                                    var msgs = Encoding.UTF8.GetBytes(list);
                                    client.Send(msgs);
                                    bool robotL = RobotModelLeft.Instance.LRobotJQRYX_Sel();
                                    Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                                    if (robotL)
                                    {
                                        if (WuLiaoCoffeeSel.Instance.RobotWarn != true)
                                        {

                                            WuLiaoCoffeeSel.Instance.RobotWarn = true;

                                        }
                                    }
                                    else
                                    {
                                        if (WuLiaoCoffeeSel.Instance.RobotWarn != false)
                                        {
                                            WuLiaoCoffeeSel.Instance.RobotWarn = false;

                                        }
                                    }
                                    ClientList clientList2 = new ClientList();
                                    clientList2.MsgBol = WuLiaoCoffeeSel.Instance;
                                    clientList2.code = 2;
                                    client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList2)));
                                }
                                //else if (ClientList.code == 6)
                                //{
                                //    MainWindow.MakingTypes = ClientList.message.type;
                                //    MainWindow.img_path = ClientList.message.Name;
                                //}
                            }
                            clientLists.RemoveAt(0);
                        }
                    }
                    catch (Exception)
                    {
                    }
                    Thread.Sleep(100);
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        public void show(Double code, string msg)
        {
            try
            {
                Thread.Sleep(100);
                switch (msg)
                {
                    case "KFBLB_IMG":
                        if (code == 0)
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup_start);
                        else
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup_end);
                        break;
                    case "NCBLB_IMG":
                        if (code == 0)
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup2_start);
                        else
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup2_end);
                        break;
                    case "LG_IMG":
                        if (code == 0)
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Lid_start);
                        else
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Lid_end);
                        break;
                    case "YGS_IMG":
                        if (code == 0)
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Cap_Cover_UP_start);
                        else
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Cap_Cover_UP_end);
                        break;
                    case "YGZ_IMG":
                        if (code == 0)
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Cap_Cover_Middle_start);
                        else
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Cap_Cover_Middle_end);
                        break;
                    case "YGX_IMG":
                        if (code == 0)
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Cap_Cover_Down_start);
                        else
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Cap_Cover_Down_end);
                        break;
                    case "DHJDJZD_IMG":
                        if (code == 0)
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LabelingMachine_start);
                        else
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LabelingMachine_end);
                        break;
                    case "KSJCS_IMG":
                        if (code == 0)
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.HotWater_start);
                        else
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.HotWater_end);
                        break;
                    case "KFJYCDD_IMG":
                        if (code == 0)
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CoffeeLight_start);
                        else
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CoffeeLight_end);
                        break;
                    case "JQRYCDD_IMG":
                        if (code == 0)
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotLight_start);
                        else
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotLight_end);
                        break;
                    case "ZBJYCDD_IMG":
                        if (code == 0)
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.IceMakerLight_start);
                        else
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.IceMakerLight_end);
                        break;
                    case "NCJYCDD_IMG":
                        if (code == 0)
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.MilkTeaMachineLight_start);
                        else
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.MilkTeaMachineLight_end);
                        break;
                    case "TBJYCDD_IMG":
                        if (code == 0)
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LabelingMachineLight_start);
                        else
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LabelingMachineLight_end);
                        break;
                    case "BYBJDD_IMG":
                        if (code == 0)
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.BYAralmLight_start);
                        else
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.BYAralmLight_end);
                        break;
                    default:
                        break;
                }
                Thread.Sleep(100);
                ClientList clientList = new ClientList();
                MessageClientList coffeelist = MessageClientList.Instance;
                coffeelist.Name = msg;
                coffeelist.type = code == 1 ? 0 : 1;
                clientList.message = coffeelist;
                clientList.code = 0;
                client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        ///   状态  0 关状态  1 开状态 2 已开机 3 上电 4 下电 5 已使能 6 下使能 7 已运行 8 继续运行 9 已报警 10 已暂停  11已停止 ,12 机械臂状态查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="Type"></param>
        public void RobotShow(string name, double Type)
        {
            try
            {
                if (name == "Reload_L")
                {
                    switch (Type)
                    {
                        //case 3:
                        //    RobotModelLeft.Instance.RobotJQRSDL_UP();//jakaAPI.power_on(ref RobotOperate.handle1);
                        //    break;
                        //case 4:
                        //    RobotModelLeft.Instance.RobotJQRXDL_UP(); //jakaAPI.power_off(ref RobotOperate.handle1);
                        //    break;
                        case 5:
                            RobotModelLeft.Instance.RobotJQRSNL_UP(); //jakaAPI.enable_robot(ref RobotOperate.handle1);
                            break;
                        case 6:
                            RobotModelLeft.Instance.RobotJQRXSNL_UP(); //jakaAPI.disable_robot(ref RobotOperate.handle1);
                            break;
                        case 7:
                            RobotModelLeft.Instance.RobotJQRYXL_UP(); // jakaAPI.program_run(ref RobotOperate.handle1);
                            break;
                        //case 8:
                        //    RobotModelLeft.Instance.RobotJQRJXYXL_UP(); //jakaAPI.program_resume(ref RobotOperate.handle1);
                        //    break;
                        case 9:
                            RobotModelLeft.Instance.RobotJQRBJQRL_UP(); // jakaAPI.clear_error(ref RobotOperate.handle1);
                            break;
                        //case 10:
                        //    RobotModelLeft.Instance.RobotJQRZTL_UP(); //jakaAPI.program_pause(ref RobotOperate.handle1);
                        //    break;
                        case 11:
                            RobotModelLeft.Instance.RobotJQRTZYXL_UP(); //jakaAPI.program_abort(ref RobotOperate.handle1);
                            break;
                    }
                    Thread.Sleep(500);
                    RobotOperate.Instance.Robot_L_Show();
                    IOMsg.Instance.RobotQK(Type);
                }
            }
            catch (Exception)
            {
            }
        }
    }
}
